package com.aplikata.evok.ui;

import java.math.BigInteger;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import com.aplikata.evok.bo.PatientTherapyComment;
import com.aplikata.evok.constants.EvokConstants;
import com.aplikata.evok.service.EvokDoctorService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;

@ManagedBean(name = "unreadCommentsBean")
@RequestScoped
public class EvokUnreadCommentsBean extends UIBean {
	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{evokDoctorService}")
	private EvokDoctorService doctorService;

	private List<Object[]> unreadList;
	private DataModel<Object[]> unreadModel = new ListDataModel<Object[]>();

	private Long counter;

	public String linkAction() {
		Object[] obj = unreadModel.getRowData();

		FacesContext.getCurrentInstance().getExternalContext().getFlash().put(EvokConstants.FLASH_PATIENT_THERAPY, obj[0]);
		FacesContext.getCurrentInstance().getExternalContext().getFlash().put(EvokConstants.FLASH_SCHEDULE_DOMAIN_PATIENT, obj[1]);
		return EvokConstants.PAGE_PATIENT_THERAPY;
	}
	
	public String getRedirectUrl(){
		if(FacesUtil.getUserFromSession().getRole().getName().equals(EvokConstants.ROLE_PATIENT)){
			return "/pages/aplikata/evok/patient/patient_therapy_list.xhtml";
		}
		return "/pages/aplikata/evok/doctor/unreadComm.xhtml";
	}

	public List<Object[]> getUnreadList() {
		if (unreadList == null) {
			unreadList = (List<Object[]>) doctorService.getUnreadComments(FacesUtil.getUserFromSession().getId());
		}
		return unreadList;
	}

	public void setUnreadList(List<Object[]> unreadList) {
		this.unreadList = unreadList;
	}

	public DataModel<Object[]> getUnreadModel() {
		if (unreadModel == null) {
			unreadModel = new ListDataModel<Object[]>();
		}
		unreadModel.setWrappedData(this.getUnreadList());
		return unreadModel;
	}

	public void setUnreadModel(DataModel<Object[]> unreadModel) {
		this.unreadModel = unreadModel;
	}

	public EvokDoctorService getDoctorService() {
		return doctorService;
	}

	public void setDoctorService(EvokDoctorService doctorService) {
		this.doctorService = doctorService;
	}

	public Long getCounter() {
		if (counter == null) {
			if (FacesUtil.getUserFromSession().getRole().getName().equals(EvokConstants.ROLE_PATIENT))
				counter = ((BigInteger) doctorService.findSingleByNamedQuery(
						PatientTherapyComment.QY_FIND_UNREAD_PATIENT_COUNT, new Object[] { FacesUtil
								.getUserFromSession().getId() })).longValue();
			else
				counter = ((BigInteger) doctorService.findSingleByNamedQuery(
						PatientTherapyComment.QY_FIND_UNREAD_DOCTOR_COUNT, new Object[] { FacesUtil
								.getUserFromSession().getId() })).longValue();
		}
		return counter;
	}

	public void setCounter(Long counter) {
		this.counter = counter;
	}
}
